package rg.edu.ap.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import rg.edu.ap.po.Seat;
import rg.edu.ap.service.SeatService;

@Controller
public class SeatController {
   @Autowired
   private SeatService seatservice;
   
   @RequestMapping("/addseat.action")
	public String addSeat(@RequestParam(value="bathid") int bathid,@RequestParam(value="merid") int merid,@RequestParam(value="seatnum") int seatnum,@RequestParam(value="seatsex") int seatsex,@RequestParam(value="seatflag") int seatflag,@RequestParam(value="seatprice") int seatprice) {
		seatservice.addSeat(bathid,merid,seatnum,seatsex,seatflag,seatprice);
	   return "setseat";
	}
   
   @RequestMapping("/makeseat.action")
	public String makeSeat(Seat seat,Model model) {
		List list = seatservice.makeSeat(seat);
		model.addAttribute("seatlist", list);
	   return "setseat";
	}
   
   @RequestMapping("/bathseat.action")
	public String bathSeat(Seat seat,Model model) {
		List list = seatservice.makeSeat(seat);
		model.addAttribute("seatlist", list);
	   return "bathseat";
	}
   
   @RequestMapping("/delseat.action")
	public String delSeat(Seat seat,Model model) {
	   seatservice.delSeat(seat);
	   List list = seatservice.makeSeat(seat);
	   model.addAttribute("seatlist", list);
	   return "setseat";
	}
      
   @RequestMapping("/appointment.action")
	public void addUserSeat(@RequestParam(value="bathid") int bathid,@RequestParam(value="merid") int merid,@RequestParam(value="userid") int userid,@RequestParam(value="usercost") int usercost,@RequestParam(value="seatnumarr") String seatnumarr) {
		ArrayList<Integer> snu = new ArrayList<Integer>();
		String[] sn;
		sn = seatnumarr.split(",");
		for(int i=0;i<sn.length;i++){
			snu.add(this.myAtoi(sn[i]));
			System.out.println(snu);
		}
	   seatservice.updateUserSeat(bathid,merid,userid,usercost,snu);
       //return "Vicinity";
	}
   public static int myAtoi(String s) {
       int a = s.length();
       if(a < 1)
           return 0;
       int i = 0;
       boolean logo = true;
      
       for(;i < a;i++){
           if(s.charAt(i) == '0')
               continue;
           else
               break;
       }
       int j = i;
       for(;j < a && j < i + 11;j++){
           if(s.charAt(j) == '0'||s.charAt(j) == '1'||s.charAt(j) == '2'||s.charAt(j) == '3'||s.charAt(j) == '4'||s.charAt(j) == '5'||s.charAt(j) == '6'||s.charAt(j) == '7'||s.charAt(j) == '8'||s.charAt(j) == '9')
               continue;
           else
               break;
       }
       if(i == j)
           return 0;
       long l = Long.valueOf(s.substring(i,j));
       int k = (int)l;
       if(logo)
           return (l == k) ? k : Integer.MAX_VALUE;
       else
           return (l == k) ? -k : Integer.MIN_VALUE;
   }
}

